package personnal.blogprinter.htmlparser;

import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;

public final class SmileyUtils {

	public static String ROOT_SMILEY_URL =
		"file:/E:/dev/workspaces/personnal/blogprinter/target/classes/smilies/";
//		"E:\\dev\\workspaces\\personnal\\blogprinter\\";
//		"http://mektoub.free.fr/blog/wp-includes/images/smilies/";

	private SmileyUtils() {
	}

	private static final Map<String, String> SMILEYS = new LinkedHashMap<String, String>();

	static {
		URL img = Thread.currentThread().getContextClassLoader().getResource("smilies/icon_wink.gif");
		String imagePath = img.toString();
		int lastSlash = imagePath.lastIndexOf('/');
		ROOT_SMILEY_URL = imagePath.substring(0, lastSlash+1);
		addSmiley(";)", 		"wink");
		addSmiley(";-)", 		"wink");
		addSmiley(":wink:", 	"wink");
		addSmiley(":)", 		"smile");
		addSmiley(":-)", 		"smile");
		addSmiley(":smile:", 	"smile");
		addSmiley(":lol:", 		"lol");
		addSmiley(":D", 		"biggrin");
		addSmiley(":-D", 		"biggrin");
		addSmiley(":grin:", 	"biggrin");
		addSmiley(":(", 		"sad");
		addSmiley(":-(", 		"sad");
		addSmiley(":sad:", 		"sad");
		addSmiley(":cry:", 		"cry");
		addSmiley(":oops:", 	"redface");
		addSmiley(":o", 		"surprised");
		addSmiley(":-o", 		"surprised");
		addSmiley(":eek:", 		"surprised");
		addSmiley(":evil:", 	"evil");
		addSmiley("8O", 		"eek");
		addSmiley("8-O", 		"eek");
		addSmiley(":shock:", 	"eek");
		addSmiley(":twisted:", 	"twisted");
		addSmiley(":???:", 		"confused");
		addSmiley(":?:", 		"question");
		addSmiley(":?", 		"confused");
		addSmiley(":-?", 		"confused");
		addSmiley(":roll:", 	"rolleyes");
		addSmiley(":!:", 		"exclaim");
		addSmiley(":idea:", 	"idea");
		addSmiley(":arrow:", 	"arrow");
		addSmiley(":mrgreen:", 	"mrgreen");
		addSmiley("8)", 		"cool");
		addSmiley("8-)", 		"cool");
		addSmiley(":cool:", 	"cool");
		addSmiley(":|", 		"neutral");
		addSmiley(":-|", 		"neutral");
		addSmiley(":neutral:", 	"neutral");
		addSmiley(":x", 		"mad");
		addSmiley(":-x", 		"mad");
		addSmiley(":mad:", 		"mad");
		addSmiley(":P", 		"razz");
		addSmiley(":-P", 		"razz");
		addSmiley(":razz:", 	"razz");
	}
	
	public static String convertSmilies(String content) {
		for (String key : SMILEYS.keySet()) {
			content = content.replaceAll(key, SMILEYS.get(key));
		}
		return content;
	}

	private static void addSmiley(String smiley, String smileyName) {
		SMILEYS.put(Pattern.quote(smiley), getImageUrl(smileyName));
	}

	private static String getImageUrl(String smileyFilename) {
		return new StringBuilder()
		.append("<img src=\"")
		.append(ROOT_SMILEY_URL)
		.append("icon_").append(smileyFilename)
		.append(".gif\"/>")
		.toString();
	}
}
